<template>
	<el-dialog v-model="visible" :title="'修改'" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px" @keyup.enter="submitHandle()">
${fieldInput}
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { validator } from '@/utils/validate'
import { doRequest } from '@/api/apiRequest'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const orgList = ref([])
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
${fieldForm}
})

const init = (id: string) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	// 初始化表单
	initForm(id)
}

// 获取信息
const initForm = (id: string) => {
	doRequest('/api/${entityVar}Select', dataForm).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
${fieldRule}
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		doRequest('/api/${entityVar}Update', dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 2000,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>